//
//  HWJoinedMoreStrategyVC.swift
//  HWanGame
//
//  Created by Apple on 2024/8/17.
//  Copyright © 2024 94Wan. All rights reserved.
//

import UIKit
import Stevia

class HWJoinedMoreStrategyVC: HWBaseViewController {

    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.register(HWStrategyListView.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.rowHeight = 272*widthScale
        return tableView
    }()
    private let topView = c_make_ex(UIView.self)
        .bgColor("#F7F8FA".toColor.alpha(0.8)).end()
    private let lineView = c_make_ex(UIView.self)
        .bgColor("#E4E5E6").end()
    private let backButton = UIIconButton(style: .leading(itemSpace: 16))
    var models:[HWGameCommunityAllListModel] = []
    var httpManager = HWGameCommunityHttpManager()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationView.isHidden = true
        view.backgroundColor = "#F7F8FA".toColor
        
        addRefresh()
        loadData()
        
        view.subviews{
            topView
            lineView
            backButton
            tableView
        }
        
        backButton.style { v in
            v.iconSize = CGSize(width: 24*widthScale, height: 24*widthScale)
            v.setImage("new_blackBack".toImage, for: .normal)
            v.setTitle("加入更多的论坛", for: .normal)
            v.setTitleColor(.black, for: .normal)
            v.titleLabel?.font = 16.font_semiBold
            v.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
        }
        
        topView.leading(0).trailing(0).height(.navi_height).top(0)
        lineView.leading(0).trailing(0).height(1).top(.navi_height)
        backButton.leading(16*widthScale).top(.statusHeight+10*widthScale).height(24*widthScale)
        tableView.leading(0).trailing(0).bottom(0).top(.navi_height + 1)
    }
    

    @objc private func clickBackButton(){
        self.navigationController?.popViewController(animated: true)
    }
}

//MARK: - UITableViewDelegate, UITableViewDataSource
extension HWJoinedMoreStrategyVC: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return models.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "HWStrategyListView", for: indexPath) as! HWStrategyListView
        cell.model = models[indexPath.row]
        cell.joinStrategy = { id in
            self.httpManager.gameCommunityFollow(id: id) { message in
                self.loadData()
            } failure: { errorString in
                self.view.showHUD(title: errorString)
            }
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let community = HWGameCommunityController()
        community.communityId = models[indexPath.row].id
        self.navigationController?.pushViewController(community, animated: true)
    }

}

//MARK: data
extension HWJoinedMoreStrategyVC {
    
    func loadData() -> Void {
        tableView.mj_footer?.isHidden = true
        httpManager.getGameCommunityAllList(type: "", keyword: "") { models in
            self.models.removeAll()
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
            for m in models {
                if !m.is_guanzhu {
                    self.models.append(m)
                }
            }
            self.tableView.reloadData()
        } failure: { errorString in
            self.models.removeAll()
            self.tableView.reloadData()
            self.tableView.mj_footer?.isHidden = true
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
        }

    }
    
    
    func addRefresh() -> () {
        
        tableView.mj_header = MJRefreshNormalHeader()
        tableView.mj_header?.setRefreshingTarget(self, refreshingAction: #selector(self.loadNewData))
        tableView.mj_footer = MJRefreshBackNormalFooter()
        tableView.mj_footer?.setRefreshingTarget(self, refreshingAction: #selector(self.loadMoreData))
        tableView.mj_footer?.isHidden = true
    }
    
    
    @objc func loadNewData() -> () {
        loadData()
    }
    
    @objc func loadMoreData() -> () {
        self.tableView.mj_footer?.endRefreshing()
        httpManager.getGameCommunityAllListFooter(type: "", keyword: "") { models in
            self.tableView.mj_footer?.endRefreshing()
            for m in models {
                if !m.is_guanzhu {
                    self.models.append(m)
                }
            }
            self.tableView.reloadData()
        } failure: { errorString in
            self.tableView.mj_footer?.endRefreshing()
        }
        
    }
}
